import json

from django.http import JsonResponse

from topic.models import Topic
from .models import Message
from django.shortcuts import render

# Create your views here.
from tools.login_dec import login_check

@login_check
def message_view(request,topic_id):
    json_str=request.body
    py_obj = json.loads(json_str)
    content = py_obj['content']
    parent_id = py_obj.get('parent_id',0)
    # 验证以下top_ip
    try:
        topic = Topic.objects.get(id=topic_id)
    except:
        result = {'code':40100,'error':'topic error'}
        return JsonResponse(result)
    user=request.myuser
    # 添加数据
    Message.objects.create(topic=topic,user_profile=user,content=content,parent_message=parent_id)
    return JsonResponse({'code':200})

